package logic

import (
	"context"

	"fmt"

	"ginmt/models/dao"
	"ginmt/models/model"
	"ginmt/pkg"
)

type MtRegionalLogic struct {
	appCtx *pkg.AppCtx
	Q      *dao.Query
}

func NewMtRegionalLogic(appCtx *pkg.AppCtx, q *dao.Query) *MtRegionalLogic {
	return &MtRegionalLogic{
		appCtx: appCtx,
		Q:      q,
	}
}

func (m *MtRegionalLogic) GetValueByCity(ctx context.Context, city string) (string, error) {
	t := m.Q.CspMtRegional
	var cityValue string
	err := t.WithContext(ctx).Where(t.City.Like(fmt.Sprintf("%%%s%%", city))).Pluck(t.MtValue, &cityValue)
	if err != nil {
		return "", err
	}
	return cityValue, nil
}

func (m *MtRegionalLogic) All(ctx context.Context) ([]*model.CspMtRegional, error) {
	return m.Q.CspMtRegional.WithContext(ctx).Find()
}

func (m *MtRegionalLogic) OnlyCity(ctx context.Context) ([]*model.CspMtRegional, error) {
	t := m.Q.CspMtRegional
	return t.WithContext(ctx).Select(t.City, t.CityPyFirst).Order(t.CityPyFirst.Asc()).Find()
}
